/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.gui.widgets;

import java.util.LinkedList;
import java.util.List;

import net.miblounge.gradecalculator.core.descriptors.modulelistitems.SelectableModuleLists;
import net.miblounge.miglayout.qt.MigLayout;

import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QWidget;

public class ModuleListsSelectorWidget
{
    private final QWidget parent;

    private final QLabel label;
    private final ModuleListsSelector selector;
    private final MigLayout layout;

    public ModuleListsSelectorWidget(final QWidget parent, final SelectableModuleLists lists)
    {
        this.parent = parent;

        layout = (MigLayout) parent.layout();

        label = new QLabel(lists.getName(), parent);
        layout.addWidget(label, "top, gaptop 9");

        selector = new ModuleListsSelector(parent, lists);
        layout.addWidget(selector, "top, wrap, grow, w ::400");
    }

    public void addSelectionListener(final SelectionsWidget selectionsWidget)
    {
        selector.addSelectionListener(selectionsWidget);
    }

    public void setSelectionEnabled(final boolean isEnabled)
    {
        if (isEnabled) {
            label.setParent(parent);
            selector.setParent(parent);
        }
        else {
            label.setParent(null);
            selector.setParent(null);
        }

        label.setVisible(isEnabled);
        selector.setVisible(isEnabled);

        if (!isEnabled) {
            selector.setSelectedItems(new LinkedList<String>());
        }
    }

    public void setItemSelected(final String name, final boolean isSelected)
    {
        selector.setItemSelected(name, isSelected);
    }

    public List<String> getSelectedItems()
    {
        return selector.getSelectedItems();
    }

    public void remove()
    {
        label.setParent(parent);
        selector.setParent(parent);
        layout.takeWidget(label);
        layout.takeWidget(selector);

        selector.dispose();
        label.dispose();
    }

}
